package com.wlyuan.open.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.wlyuan.basic.api.IGoodsService;
import com.wlyuan.basic.api.IGoodsTypeService;
import com.wlyuan.basic.dto.goods.GoodsTypeCreationDTO;
import com.wlyuan.basic.dto.goods.GoodsTypeFilterDTO;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.open.assembler.GoodsAssembler;
import com.wlyuan.open.basic.dto.*;
import com.wlyuan.open.basic.vo.OpenGoodsDetailVO;
import com.wlyuan.open.exception.GoodsTypeNoFoundException;
import com.wlyuan.open.translator.GoodsTranslator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author yuanjie
 */
@Api(tags = "物品")
@Validated
@RequestMapping("/api/goods")
@RestController
public class OpenGoodsController {
    @DubboReference(check = false)
    private IGoodsTypeService goodsTypeService;
    @DubboReference(check = false)
    private IGoodsService goodsService;


    @ApiOperation("新增物品类型")
    @PostMapping("/type")
    @OpenApi
    public Long createGoodsType(@RequestBody @Valid OpenGoodsTypeCreationDTO openGoodsType) {
        GoodsTypeCreationDTO goodsType = GoodsAssembler.assemble(openGoodsType);
        var detail = goodsTypeService.create(goodsType);
        return detail.getGoodsTypeId();
    }

    @ApiOperation("修改物品类型")
    @PutMapping("/type")
    @OpenApi
    public void updateGoodsType(@RequestBody @Valid OpenGoodsTypeRenameDTO openGoodsTypeRename) {
        //根据类型名称获取物品类型
        GoodsTypeFilterDTO goodsTypeFilter = new GoodsTypeFilterDTO();
        goodsTypeFilter.setName(openGoodsTypeRename.getOldName());
        goodsTypeFilter.setStatus(NamedStatus.ENABLED.getValue());
        var result = goodsTypeService.search(goodsTypeFilter);
        if (result != null && CollectionUtils.isNotEmpty(result.getRecords())) {
            goodsTypeService.update(GoodsAssembler.assemble(result.getRecords().get(0).getGoodsTypeId(), openGoodsTypeRename));
        } else {
            throw new GoodsTypeNoFoundException("旧物品类型不存在");
        }
    }

    @ApiOperation("新增物品")
    @PostMapping("/")
    @OpenApi
    public OpenGoodsDetailVO createGoods(@RequestBody @Valid OpenGoodsCreationDTO openGoodsCreation) {
        var goodsCreation = GoodsAssembler.assemble(openGoodsCreation);
        var goods = goodsService.create(goodsCreation);
        return GoodsTranslator.translate(goods);
    }

    @ApiOperation("修改物品")
    @PutMapping("/")
    @OpenApi
    public void updateGoods(@RequestBody @Valid OpenGoodsUpdateDTO openUpdate) {
        var goodsUpdate = GoodsAssembler.assemble(openUpdate);
        goodsService.update(goodsUpdate);

    }

    @ApiOperation("根据名称和编码获取最新物品")
    @PostMapping("/latest")
    public OpenGoodsDetailVO search(@RequestBody OpenGoodsLatestDTO latest) {
        var filter = GoodsAssembler.assemble(latest);
        var goods = goodsService.search(filter);
        if (goods != null && CollectionUtil.isNotEmpty(goods.getRecords())) {
            return GoodsTranslator.translate(goods.getRecords().get(0));
        }
        return null;
    }


}
